package com.fit.pages;


import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;

import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.interfaces.User;
import com.fit.service.Users;
import com.fit.util.Security;



public class Login {

	
	private String userName;
	
	@Property
	private String password;
	
	@Inject
	@Property
	private Users users;
	
	@SessionState
	private User user;
	
	@Component(id="loginForm")
	private Form loginForm;
	
	@Inject
	private Messages messages;
	
	public String getUserName() {
		return userName;
	}

	

	public void setUserName(String userName) {
		this.userName = userName;
	}


	
	void onValidateForm(){
		User authenticatedUser = Security.authenticate(userName, password , users);
		if(authenticatedUser != null){
			user = authenticatedUser;
		}else{
			loginForm.recordError(messages.get("authentication-failed"));
		}
	}
	
	Object onSuccess()
	{
		return Index.class;
	}
	
//	Object onSubmitFromLoginForm(){
//	
//	User authenticatedUser = Security.authenticate(userName, password, users);
//	if (authenticatedUser != null){
//		user = authenticatedUser;
//		return Index.class;
//	} else return Registration.class;
//
//	}



//	@OnEvent
//	Object onSubmit(){
//		System.out.println("form was submited");
//		Class nextPage = null;
//		User authenticatedUser = Security.authenticate(userName, password, users);
//		if(authenticatedUser != null){
//			user = authenticatedUser;
//			nextPage = Index.class;
//		} else {
//		
//		nextPage = Registration.class;
//		}
//		return nextPage;
//	}
}
